/**********************************************************
 * PendingAccountsAction.java.java
 * Created by Ashutosh
 * Last modified Date: Nov 18, 2008 10:49:01 AM by Nishan
 * Revision: <svn revision #>
 * Version : 0.1
 * Copyright (c) 2008 - 2009 cyberThink Infotech Pvt. Ltd., All rights reserved.
 **********************************************************/

package com.printeverywhere.action;

import static com.printeverywhere.util.AttributeConstants.CITY;
import static com.printeverywhere.util.AttributeConstants.COMPANY;
import static com.printeverywhere.util.AttributeConstants.EMAIL;
import static com.printeverywhere.util.AttributeConstants.FIRSTNAME;
import static com.printeverywhere.util.AttributeConstants.LASTNAME;
import static com.printeverywhere.util.AttributeConstants.OTHER;
import static com.printeverywhere.util.AttributeConstants.PHONE;
import static com.printeverywhere.util.AttributeConstants.STATE;
import static com.printeverywhere.util.AttributeConstants.STREET;
import static com.printeverywhere.util.AttributeConstants.USER_ID;
import static com.printeverywhere.util.AttributeConstants.WEBSITEADDRESS;
import static com.printeverywhere.util.AttributeConstants.ZIP;
import static com.printeverywhere.util.ButtonConstants.ACCOUNTMANAGER_SUBMIT;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.apache.struts2.interceptor.ServletRequestAware;

import com.printeverywhere.delegator.CommonFunctionDelegator;
import com.printeverywhere.delegator.UserAccountsDelegator;
import com.printeverywhere.delegator.UserServiceDelegator;
import com.printeverywhere.dto.AccountManagerOption;
import com.printeverywhere.dto.PendingAccounts;
import com.printeverywhere.dto.User;
import com.printeverywhere.util.DisplayConstants;
import com.printeverywhere.util.SearchCriteria;

/**
 * The Class PendingAccountsAction.
 */
public class PendingAccountsAction extends AdminEngineManagement implements ServletRequestAware {

	/** Logger for this class. */
	private static final Logger logger = Logger.getLogger(PendingAccountsAction.class);

	/** The Constant serialVersionUID. */
	private static final long serialVersionUID = 1L;

	/** The account search list. */
	private List<AccountManagerOption> accountSearchList;

	/** The operation. */
	private String operation;

	/** The search string. */
	private String searchString;

	/** The list. */
	private List<User> list;

	/** The group. */
	private String group;

	/** The criteria. */
	private String criteria;

	/** The user accounts count. */
	PendingAccounts pendingAccountsCount;

	/** The selected user deleted. */
	private String[] selectedUserDeleted;

	/** The request. */
	private HttpServletRequest request;

	private int accountDetailid;

	private User accountDetailList;

	private User activeUser;

	private String deleteId = null;

	/**
	 * @return the accountDetailList
	 */
	public User getAccountDetailList() {
		return accountDetailList;
	}

	/**
	 * @param accountDetailList the accountDetailList to set
	 */
	public void setAccountDetailList(User accountDetailList) {
		this.accountDetailList = accountDetailList;
	}

	/**
	 * @return the accountDetailid
	 */
	public int getAccountDetailid() {
		return accountDetailid;
	}

	/**
	 * @param accountDetailid the accountDetailid to set
	 */
	public void setAccountDetailid(int accountDetailid) {
		this.accountDetailid = accountDetailid;
	}

	/* (non-Javadoc)
	 * @see com.opensymphony.xwork2.ActionSupport#execute()
	 */
	public String execute() throws Exception{
		if (logger.isDebugEnabled()) {
			logger.debug("execute() - start"); //$NON-NLS-1$
		}

		populateAccountSearchList();

		if (logger.isDebugEnabled()) {
			logger.debug("execute() - end"); //$NON-NLS-1$
		}
		return SUCCESS;
	}

	public String searchUser() throws Exception{

		populateAccountSearchList();
		return SUCCESS;
	}

	public String activeUser() throws Exception{

		populateAccountSearchList();

		if(request.getParameter("active") != null && request.getParameter("delete") != null){
				addActionError("One time you can Perform only one action");
				return INPUT;
			}
			if (request.getParameter("active") != null) {
				String[] checkedId = request.getParameterValues("active");
				for (String chkid : checkedId) {
					activeUser = UserServiceDelegator.getUser(Integer.parseInt(chkid));
					activeUser.setActive(true);
					UserServiceDelegator.updateUser(activeUser);
				}
				populateAccountSearchList();
				return SUCCESS;
			}else if(request.getParameter("delete") != null){
				String[] deleteCheckedId = request.getParameterValues("delete");
				for (String chkid : deleteCheckedId) {
					if (deleteId == null) {
						deleteId = chkid;
					} else {
						deleteId = deleteId + " , " + chkid;
					}
				}
//				CommonFunctionDelegator.executeQuery("DELETE FROM User user WHERE id IN (" + deleteId + ")");
				System.out.println("DELETE Selected .... " + "DELETE FROM User user WHERE id IN (" + deleteId + ")");
				populateAccountSearchList();
				return SUCCESS;
			}


		addActionError("Please select check box for enable or delete Pending Accounts");
		return INPUT;
	}

	/**
	 * Populate account search list.
	 */
	public void populateAccountSearchList(){
		if (logger.isDebugEnabled()) {
			logger.debug("populateAccountSearchList() - start"); //$NON-NLS-1$
		}

		accountSearchList = new ArrayList<AccountManagerOption>();
		accountSearchList.add(new AccountManagerOption(USER_ID,DisplayConstants.ACCOUNT));
		accountSearchList.add(new AccountManagerOption(EMAIL,DisplayConstants.EMAIL));
		accountSearchList.add(new AccountManagerOption(WEBSITEADDRESS,DisplayConstants.WEBSITE));
		accountSearchList.add(new AccountManagerOption(PHONE,DisplayConstants.PHONE));
		accountSearchList.add(new AccountManagerOption(FIRSTNAME,DisplayConstants.FIRST_NAME));
		accountSearchList.add(new AccountManagerOption(LASTNAME,DisplayConstants.LAST_NAME));
		accountSearchList.add(new AccountManagerOption(COMPANY,DisplayConstants.COMPANY));
		accountSearchList.add(new AccountManagerOption(STREET,DisplayConstants.STREET));
		accountSearchList.add(new AccountManagerOption(CITY,DisplayConstants.CITY));
		accountSearchList.add(new AccountManagerOption(STATE,DisplayConstants.STATE));
		accountSearchList.add(new AccountManagerOption(ZIP,DisplayConstants.ZIP));

		SearchCriteria searchCriteria = new SearchCriteria();
		searchCriteria.setAttribute("active", Boolean.FALSE);
		if(operation != null){
			searchCriteria.setAttribute(criteria, searchString);
			list = UserServiceDelegator.getCaseInsensitiveListOfSearch(searchCriteria);
		}else if(group != null && !group.equals(OTHER)){
			searchCriteria.setAttribute(EMAIL, group);
			list = UserServiceDelegator.getCaseInsensitiveList(searchCriteria);
		}else if(group != null && group.equals(OTHER)){
			list = UserServiceDelegator.getUserListStartsWithDigit();
		}else{
			list = UserServiceDelegator.getCaseInsensitiveList(searchCriteria);
		}
		pendingAccountsCount = UserAccountsDelegator.findPendingAccounts();
		if(selectedUserDeleted != null && selectedUserDeleted.length>0){
			for (String user : selectedUserDeleted) {
				// TODO TO DELETE ALL SELECTED USERS
			}
		}

		if (logger.isDebugEnabled()) {
			logger.debug("populateAccountSearchList() - end"); //$NON-NLS-1$
		}
	}

	public String accountDetails() throws Exception{

		accountDetailid = Integer.parseInt(request.getParameter("accountDetailid"));
		accountDetailList = UserServiceDelegator.getUser(accountDetailid);
		return SUCCESS;
	}

	/**
	 * @return the accountSearchList
	 */
	public List<AccountManagerOption> getAccountSearchList() {
		return accountSearchList;
	}

	/**
	 * @param accountSearchList the accountSearchList to set
	 */
	public void setAccountSearchList(List<AccountManagerOption> accountSearchList) {
		this.accountSearchList = accountSearchList;
	}

	/**
	 * @return the operation
	 */
	public String getOperation() {
		return operation;
	}

	/**
	 * @param operation the operation to set
	 */
	public void setOperation(String operation) {
		this.operation = operation;
	}

	/**
	 * @return the searchString
	 */
	public String getSearchString() {
		return searchString;
	}

	/**
	 * @param searchString the searchString to set
	 */
	public void setSearchString(String searchString) {
		this.searchString = searchString;
	}

	/**
	 * @return the list
	 */
	public List<User> getList() {
		return list;
	}

	/**
	 * @param list the list to set
	 */
	public void setList(List<User> list) {
		this.list = list;
	}

	/**
	 * @return the group
	 */
	public String getGroup() {
		return group;
	}

	/**
	 * @param group the group to set
	 */
	public void setGroup(String group) {
		this.group = group;
	}

	/**
	 * @return the criteria
	 */
	public String getCriteria() {
		return criteria;
	}

	/**
	 * @param criteria the criteria to set
	 */
	public void setCriteria(String criteria) {
		this.criteria = criteria;
	}


	public PendingAccounts getPendingAccountsCount() {
		return pendingAccountsCount;
	}

	public void setPendingAccountsCount(PendingAccounts pendingAccountsCount) {
		this.pendingAccountsCount = pendingAccountsCount;
	}

	/**
	 * @return the selectedUserDeleted
	 */
	public String[] getSelectedUserDeleted() {
		return selectedUserDeleted;
	}

	/**
	 * @param selectedUserDeleted the selectedUserDeleted to set
	 */
	public void setSelectedUserDeleted(String[] selectedUserDeleted) {
		this.selectedUserDeleted = selectedUserDeleted;
	}

	public void setServletRequest(HttpServletRequest request) {
		this.request = request;
	}

	public User getActiveUser() {
		return activeUser;
	}

	public void setActiveUser(User activeUser) {
		this.activeUser = activeUser;
	}

	public String getDeleteId() {
		return deleteId;
	}

	public void setDeleteId(String deleteId) {
		this.deleteId = deleteId;
	}
}